package com.zzp.zjs_bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzp.zjs_bos.domain.Region;
import com.zzp.zjs_bos.utils.PinYin4jUtils;
import com.zzp.zjs_bos.web.action.base.BaseAction;


@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region> {
	
	//需要上传的文件
	private File myFile;
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	
	//上传文件的文件名
	private String myFileFileName;
	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}


	public String importFile() throws FileNotFoundException, IOException{
		Workbook workbook = null;
		String f = "1";
		try {
			List<Region> list = new ArrayList<>();
			if(myFileFileName.endsWith(".xlsx")){
				//加载上传的文件
				workbook = new XSSFWorkbook(new FileInputStream(myFile));
			}else{
				workbook = new HSSFWorkbook(new FileInputStream(myFile));
			}
			
			//加载第一个sheet
			Sheet sheetAt = workbook.getSheetAt(0);
			
			//遍历sheet
			for (Row row : sheetAt) {
				int rowNum = row.getRowNum();
				if(rowNum == 0){
					continue;
				}
				String id = row.getCell(0).getStringCellValue();
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				String postcode = row.getCell(4).getStringCellValue();
				
				Region region = new Region(id, province, city, district, postcode);
				
				String shortcode = province + city + district;
				String[] headByString = PinYin4jUtils.getHeadByString(shortcode);
				
				shortcode = StringUtils.join(headByString,"");
				
				String[] stringToPinyin = PinYin4jUtils.stringToPinyin(city);
				
				String citycode = StringUtils.join(stringToPinyin);
				
				region.setShortcode(shortcode);
				region.setCitycode(citycode);
				list.add(region);
			}
				regionService.saveBatch(list);
			
		} catch (Exception e) {
			e.printStackTrace();
			f = "0";
		  }
		
		ServletActionContext.getResponse().getWriter().write(f);

		return NONE;
	}
	
	public String pageQuery(){
		regionService.pageQuery(pageBean);
		String[] excludes = new String[]{"subareas"};
		this.writePageBean2Json(pageBean, excludes);
		return NONE;
	}
	
	public String findAll(){
		List<Region> list = regionService.fingAll();
		String[] excludes = new String[]{"postcode","shortcode","citycode","subareas"};
		this.writeList2Json(list, excludes);
		return NONE;
	}
}
